package xyz.xiaomeiqiu.study;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import xyz.xiaomeiqiu.study.domain.Student;
import xyz.xiaomeiqiu.study.util.JdbcConn;
import xyz.xiaomeiqiu.study.util.JdbcConnTransaction;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootApplication
@RestController
public class App {

    @Resource
    private JdbcTemplate jdbcTemplate;

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

    @GetMapping("/jdbcTest")
    public Map jdbcTest() throws SQLException {
        HashMap<Object, Object> datas = new HashMap<>();
        JdbcConn.test();
        datas.put("result", "ok");
        return datas;
    }

    @GetMapping("/jdbcTransactionTest")
    public Map jdbcTransactionTest() throws SQLException {
        HashMap<Object, Object> datas = new HashMap<>();
        JdbcConnTransaction.test();
        datas.put("result", "ok");
        return datas;
    }

    @GetMapping("/jdbcTemplate/students")
    public List<Student> test() {
        RowMapper<Student> rowMapper = new BeanPropertyRowMapper<>(Student.class);
        List<Student> studentList = jdbcTemplate.query("select * from Student", rowMapper);
        for (Student student : studentList) {
            System.out.println(student.getName());
        }
        return studentList;
    }
}
